// Copyright (c) MatrixFramework. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

using System.Collections.Generic;

// Factory methods in generic types are acceptable for object creation patterns
#pragma warning disable CA1000 // Do not declare static members on generic types

namespace MatrixFramework.Core.Results;

/// <summary>
/// 带数据的操作结果类
/// </summary>
/// <typeparam name="T">数据类型</typeparam>
public class Result<T> : ResultBase
{
    /// <summary>
    /// 结果数据
    /// </summary>
    public T? Data { get; private set; }

    /// <summary>
    /// 是否失败
    /// </summary>
    public bool IsFailure => !IsSuccess;

    /// <summary>
    /// 私有构造函数
    /// </summary>
    private Result() { }

    /// <summary>
    /// 创建成功结果
    /// </summary>
    /// <param name="data">数据</param>
    /// <returns>成功结果</returns>
    public static Result<T> Success(T data)
    {
        return new Result<T>
        {
            IsSuccess = true,
            Data = data
        };
    }

    /// <summary>
    /// 创建失败结果
    /// </summary>
    /// <param name="error">错误信息</param>
    /// <returns>失败结果</returns>
    public static Result<T> Failure(string error)
    {
        var result = new Result<T>
        {
            IsSuccess = false,
            Data = default
        };
        result.AddError(error);
        return result;
    }

    /// <summary>
    /// 创建失败结果
    /// </summary>
    /// <param name="errors">错误信息列表</param>
    /// <returns>失败结果</returns>
    public static Result<T> Failure(IEnumerable<string> errors)
    {
        var result = new Result<T>
        {
            IsSuccess = false,
            Data = default
        };
        result.AddErrors(errors ?? new List<string>());
        return result;
    }

    /// <summary>
    /// 创建失败结果
    /// </summary>
    /// <param name="error">错误信息</param>
    /// <returns>失败结果</returns>
    public new static Result<T> Error(string error)
    {
        return Failure(error);
    }

    /// <summary>
    /// 隐式转换操作符 - 从数据转换为成功结果
    /// </summary>
    /// <param name="data">数据</param>
    /// <returns>成功结果</returns>
    public static implicit operator Result<T>(T data)
    {
        return Success(data);
    }

    /// <summary>
    /// 从数据创建成功结果（运算符备用方法）
    /// </summary>
    /// <param name="data">数据</param>
    /// <returns>成功结果</returns>
    public static Result<T> FromT(T data)
    {
        return Success(data);
    }

    /// <summary>
    /// 从数据转换为成功结果
    /// </summary>
    /// <param name="data">数据</param>
    /// <returns>成功结果</returns>
    public static Result<T> ToResult(T data)
    {
        return Success(data);
    }
}

#pragma warning restore CA1000 // Do not declare static members on generic types